package com.ibiaoke.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.ibiaoke.common.configs.AppAliPayConfigs;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class AppAliPay {

    /**
     * 移动支付
     *
     */
    public String aliPay(String amount, Map<String, Object> body) {
        AlipayClient alipayClient = new DefaultAlipayClient(AppAliPayConfigs.getGateWay(), AppAliPayConfigs.getAppID(), AppAliPayConfigs.getPrivateKey(), AppAliPayConfigs.FORMAT, AppAliPayConfigs.CHARSET, AppAliPayConfigs.getAliPublicKey(), AppAliPayConfigs.SIGNTYPE);

        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();

        model.setBody("让我来果源水果");
        model.setSubject(body.get("subject").toString());
        model.setOutTradeNo(body.get("out_trade_no").toString());
        model.setTimeoutExpress("30m");
        model.setTotalAmount(amount);
        model.setProductCode("QUICK_MSECURITY_PAY");

        request.setBizModel(model);
        request.setNotifyUrl((String)body.get("notify_url"));

        String orderStr = "";
        try {
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            orderStr = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return orderStr;
    }
}
